package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.TimerTask;

import message.response.MessageResponse;

public class ProxyNotifier extends TimerTask {

	private String proxyHost;
	private int proxyUdpPort;

	private DatagramSocket socket;
	private DatagramPacket packet;

	public ProxyNotifier(String proxyHost, int proxyUdpPort, String message) {

		this.proxyHost = proxyHost;
		this.proxyUdpPort = proxyUdpPort;
		
		byte[] buf = message.getBytes();
		InetAddress address = null;
		
		try {
			socket = new DatagramSocket();
			address = InetAddress.getByName(this.proxyHost);
			
			packet = new DatagramPacket(buf, buf.length, address, this.proxyUdpPort);
			
		} catch (SocketException e) {
			System.out.println(new MessageResponse("Error: An unexpected error occurred!"));
		} catch (UnknownHostException e) {
			System.out.println(new MessageResponse("Error: An unexpected error occurred!"));
		}
	}

	@Override
	public void run() {
		try {
			socket.send(packet);
		} catch (IOException e) {
			System.out.println(new MessageResponse("Error: An unexpected error occurred!"));
		}
	}

}
